<?php
App::import('Helper', 'Store.StoreAppHelper');
class StoreHelper extends StoreAppHelper{
	var $helpers = array('Html', 'Form','Tree','Session','Number','Javascript');
	public function products($group_id=null,$params=null){		    	
    	$products_items = $this->ComponentStore->get_products($group_id,$params);
		return $products_items;
	}
	
	public function categories($only_ul = false,$type = null){	
		
    	$categories_ = $this->ComponentStore->get_categories($type);
		switch($type){
	    	case 'main':
				$categories = $this->Tree->generate($categories_, array('plugin'=>'store','model'=>'StoreCategory','element'=>'store_categories_export'));
				break;
	    	case 'list':
	    		$categories = $this->Tree->generate($categories_, array('plugin'=>'store','model'=>'StoreCategory','element'=>'store_categories_list','type' => false ,'itemType' => false,'depth'=>2));
	    		break;
	    	case 'selectbox':
	    		return $categories_;	    		
	    		break;
	    	default:
				$categories = ($only_ul)? $this->Tree->generate($categories_, array('plugin'=>'store','model'=>'StoreCategory','element'=>'store_categories_menu')) : $this->Tree->generate($categories_, array('id'=>'tree','class'=>'filetree treeview','plugin'=>'store','model'=>'StoreCategory','element'=>'store_categories'));
		}
		if($only_ul) return $categories;
		return $categories;
	}
	
	public function products_mdels(){	
		switch($return){
			default:
				return $this->ComponentStore->get_models();				
		}
	}

	
	public function admin_categories(){
    	$categories_ = $this->ComponentStore->get_categories();
		$categories = $this->Tree->generate($categories_, array('id'=>'tree','class'=>'filetree treeview','plugin'=>'store','model'=>'StoreCategory','element'=>'store_admin_categories'));
		return  $categories;
	}	
	
	public function price($price,$places=2,$before="",$after=null,
		$product_currency=null,$product_exchange=null,
		$default_currency=null,$default_exchange=null,
		$decimals = '.', $thousands = ' '
	){
		$price = str_replace(',', '.', $price);

		$selected_currency = $this->Session->read('Store.selected_currency');
		$selected_exchange = $this->Session->read('Store.selected_exchange');
		$selected_symbol = $this->Session->read('Store.selected_symbol');
		
		$selected_currency = (!empty($selected_currency))?$selected_currency:$default_currency;
		$selected_exchange = (!empty($selected_exchange))?$selected_exchange:$default_exchange;
		
		if($selected_exchange != 0 && $product_exchange != 0 ){
			$price = $product_exchange * $price / $selected_exchange;
			$price = $this->Number->format($price, array(
			    'places' => $places,
			    'before' => '<span class="product-view-currency"> '.((is_null($after))?$selected_symbol:$after).'</span>',
				'after'	=> '',
			    'escape' => false,
			    'decimals' => $decimals,
			    'thousands' => $thousands
			));
		}
		return $price;
	}
	
	public function currency(){
		$currency = $this->Currency->get_list();
		if(is_array($currency) && count($currency) > 1 ){
			$selected_currency = $this->Session->read('Store.selected_currency');
			$form = $this->Form->create('StoreCurrency',array('url'=>array('plugin'=>'store','controller'=>'currencies','action'=>'change'),'id'=>'StoreCurrencyCahnegForm'));
			$form .= $this->Form->input('selected_currency',array('options'=>$currency,'selected'=>$selected_currency,'label'=>__d("store","Selected Currency",true)));
			$form .= $this->Form->end(__d("store","Change",true));		
			return $form;
		}else{
			return null;
		}
	}

	public function cart_info(){
		$items = $this->Session->read('Store.Cart.Items');
		$delivery = $this->Session->read('Store.Cart.Delivery');
		return $this->View->element('cart_info_top',array('plugin'=>'store','items'=>$items,'delivery'=>$delivery));
		
	}
}